//package com.atguigu.gulimall.search.config;
//
//import lombok.Data;
//import org.apache.http.HttpHost;
//import org.elasticsearch.client.Node;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.stereotype.Component;
//
//import java.util.Arrays;
//import java.util.Objects;
//
///**
// * Copyright (c) 2019-2029 星大陆 All Rights Reserved
// * <p>
// * Project: dayu
// * Package: com.yintech.bigdata.dayu.common
// * Version: V 1.0
// * <p>
// * Created by guoxiaolong on 2020/11/27 15:30
// */
//@Component
//@ConfigurationProperties(prefix = "spring.es")
//@Data
//public class ElasticsearchConfig {
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);
//
//    private static final int ADDRESS_LENGTH = 2;
//    private String nodes;
//    private String name;
//    private Integer requestTimeout;
//    private Integer socketTimeout;
//    private Integer connectTimeout;
//
//    @Bean(name = "highLevelClient")
//    public RestHighLevelClient highLevelClient() {
//        String[] split = nodes.split(",");
//        HttpHost[] hosts = Arrays.stream(split)
//                .map(this::makeHttpHost)
//                .filter(Objects::nonNull)
//                .toArray(HttpHost[]::new);
//        RestClientBuilder restClientBuilder = RestClient.builder(hosts);
//
//        // 设置一个监听器，每次节点出现故障时都会收到通知，以防需要采取措施，
//        // 当启用故障嗅探时在内部使用。
//        restClientBuilder.setFailureListener(new RestClient.FailureListener() {
//            @Override
//            public void onFailure(Node node) {
//                LOGGER.error("init client error, host:{}", node);
//            }
//        });
//        // 设置允许修改默认请求配置的回调
//        //（例如请求超时，身份验证或 org.apache.http.client.config.RequestConfig.Builder 允许设置的任何内容）。
//        restClientBuilder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
//                .setConnectionRequestTimeout(requestTimeout)
//                .setSocketTimeout(socketTimeout)
//                .setConnectTimeout(connectTimeout));
//
//        return new RestHighLevelClient(restClientBuilder);
//    }
//
//    /**
//     * 根据配置创建 HttpHost
//     *
//     * @param s
//     * @return
//     */
//    private HttpHost makeHttpHost(String s) {
//        String[] address = s.split(":");
//        if (address.length == ADDRESS_LENGTH) {
//            String ip = address[0];
//            int port = Integer.parseInt(address[1]);
//            return new HttpHost(ip, port, "http");
//        } else {
//            return null;
//        }
//    }
//}
